﻿using IServices;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Entity;
using YL.Core.Entity.Common;
using YL.Core.Entity.Fluent.Validation;
using YL.NetCore.Attributes;
using YL.NetCore.NetCoreApp;
using YL.Utils.Extensions;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Method.SmartMeter.Controllers
{
    public class UserClientController : BaseController
    {
        private readonly ISys_userclientServices _userclientServices;
        private readonly IMeter_ClientInfoServices _clientInfoServices;


        public UserClientController(ISys_userclientServices userclientServices, IMeter_ClientInfoServices clientInfoServices)
        {
            _userclientServices = userclientServices;
            _clientInfoServices = clientInfoServices;
        }

        [HttpGet]
        [OperationLog(LogType = LogType.select)]
        public ContentResult List(string userId)
        {
            var sd = _userclientServices.PageList(userId);
            return Content(sd);
        }
        [HttpGet]
        [OperationLog(LogType.delete)]
        public IActionResult Delete(string id)
        {
            Sys_userclient sys_Userclient=_userclientServices.QueryableToEntity(s=>s.Id== SqlFunc.ToInt64(id));
            
            var flag2= _clientInfoServices.Update(new ClientInfo { ClientId = sys_Userclient.ClientId, IsBind = 0 }, 
            c => new { c.IsBind });
            if (flag2)
            {
                var flag1 = _userclientServices.Delete(s => s.Id == SqlFunc.ToInt64(id));
                return BootJsonH(flag1 ? (flag1, PubConst.Delete1) : (flag1, PubConst.Delete2));
            }
            else {
                return BootJsonH((flag2, PubConst.Delete2));
            }
            
        }
    }
}